package org.example.pet.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.pet.annotation.RequiresRole;
import org.example.pet.common.Result;
import org.example.pet.dto.PetAddDTO;
import org.example.pet.dto.PetInfoResponseDTO;
import org.example.pet.dto.PetPageQueryDTO;
import org.example.pet.pojo.Pet;
import org.example.pet.pojo.PetBreed;
import org.example.pet.service.PetBreedService;
import org.example.pet.service.PetService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private PetService petService;
    @Autowired
    private PetBreedService petBreedService;

    /**
     * 宠物信息分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result<Page<PetInfoResponseDTO>> page(int page, int pageSize) {
        log.info("宠物信息分页查询...");
        Page<PetInfoResponseDTO> pageRes = petService.page(page, pageSize);
        return Result.success(pageRes);
    }

    /**
     * 宠物信息分页查询（带过滤条件）
     * @param petPageQueryDTO
     * @return
     */
    @PostMapping("/page")
    public Result<Page<PetInfoResponseDTO>> page(@RequestBody PetPageQueryDTO petPageQueryDTO) {
        log.info("宠物信息分页查询...");
        Page<PetInfoResponseDTO> pageRes = petService.page(petPageQueryDTO);
        return Result.success(pageRes);
    }

    /**
     * 添加宠物
     * @param dto
     * @return
     */
    @PostMapping("/addPet")
    @RequiresRole("ADMIN")
    public Result<String> addPet(@RequestBody PetAddDTO dto) {
        log.info("添加宠物...");
        petService.addPet(dto);
        return Result.success("添加宠物成功");
    }

    /**
     * 删除宠物
     * @param petId
     * @return
     */
    @PostMapping("/delPet/{petId}")
    @RequiresRole("ADMIN")
    public Result<String> delPet(@PathVariable("petId") Long petId) {
        log.info("删除宠物...");
        petService.removeById(petId);
        return Result.success("删除宠物成功");
    }

    /**
     * 修改宠物信息
     * @param pet
     * @return
     */
    @PostMapping("/update")
    @RequiresRole("ADMIN")
    public Result<String> update(@RequestBody Pet pet) {
        log.info("修改宠物信息...");
        petService.updateById(pet);
        return Result.success("修改宠物信息成功");
    }

    /**
     * 获取宠物详情
     * @param petId
     * @return
     */
    @GetMapping("/getPetDetail/{petId}")
    public Result<PetInfoResponseDTO> getPetDetail(@PathVariable("petId") Long petId) {
        log.info("获取宠物详情...");
        PetInfoResponseDTO petDetail = petService.getPetDetail(petId);
        return Result.success(petDetail);
    }
    
    @GetMapping("/breed/list")
    public Result<List<PetBreed>> getBreedList() {
        log.info("获取宠物品种列表...");
        List<PetBreed> petBreeds = petBreedService.list();
        return Result.success(petBreeds);
    }
}
